package com.dg.contorller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dg.core.controller.BaseController;
import com.dg.pojo.Dept;
import com.dg.pojo.Person;
import com.dg.service.DeptService;
import com.dg.service.PersonService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class TestController extends BaseController {

    @Resource(name = "deptService")
    private DeptService deptService;

    @Resource(name = "personService")
    private PersonService personService;

    @RequestMapping("/helloworld")
    public String test() {
        return "helloworld";
    }

    @RequestMapping("/showdept")
    public String showdept() {

        List<Dept> list = deptService.findDept();

        Dept d = list.get(0);
        System.out.println(d.getDeptid() + " " + d.getDname() + " " + d.getLoc());

        return list.toString();
    }


    //提供接口给：vue element-ui
    @RequestMapping("/getPersonDataTest")
    public String getPersonDataTest() {
        pd = this.getParamsData();
        int pagesize = new Integer(pd.getString("pagesize"));
        int pagenum = new Integer(pd.getString("pagenum"));

        PageHelper.startPage(pagenum, pagesize);    //分页
        List<Person> list = personService.findAll();
        PageInfo<Person> pageInfo = new PageInfo<Person>(list);
        long total = pageInfo.getTotal(); //获取总记录数

        JSONObject json = new JSONObject();
        json.put("code", 200);
        json.put("msg", "查询成功");
        json.put("data", JSONArray.parseArray(JSON.toJSONString(list)));
        json.put("total", total);

        return json.toString();
    }

}
